import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Random;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.获取表单里的参数
//        String username = request.getParameter("username");
//        String password = request.getParameter("password");
//        String vcode = request.getParameter("vcode");
//
//        //2.判断验证码
//        String code = (String) request.getSession().getAttribute("code");//从session中取出之前生成的验证码
//
//        if (!code.equals(vcode)){//如果验证码不正确
//            request.setAttribute("msg","验证码错误");
//            request.getRequestDispatcher("/register.jsp").forward(request,response);
//        }
//        //3.判断当前用户名是否已经注册
//        ServletContext context = getServletContext();
//        Object userMapInContext = context.getAttribute("userMap");
//        HashMap<String,String> userMap = new HashMap();
//
//        if (userMapInContext==null){
//            userMap.put(username,password);
//            context.setAttribute("userMap",userMap);
//            request.setAttribute("msg","注册成功");
//            request.getRequestDispatcher("/login.jsp").forward(request,response);
//        }else {
//            userMap = (HashMap<String, String>) userMapInContext;
//
//            if (userMap.containsKey(username)){
//                // 当前用户名以存在
//                request.setAttribute("msg","当前用户名已存在");
//                request.getRequestDispatcher("/register.jsp").forward(request,response);
//            }else {
//                userMap.put(username,password);
//                context.setAttribute("userMap",userMap);
//                request.setAttribute("msg","注册成功");
//                request.getRequestDispatcher("/login.jsp").forward(request,response);
//            }
//        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
//        1.获取表单里的参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String vcode = request.getParameter("vcode");

        //2.判断验证码
        String code = (String) request.getSession().getAttribute("code");//从session中取出之前生成的验证码

        if (!code.equals(vcode)){//如果验证码不正确
            request.setAttribute("msg","验证码错误");
            request.getRequestDispatcher("/register.jsp").forward(request,response);
        }
        //3.判断当前用户名是否已经注册
        ServletContext context = getServletContext();
        Object userMapInContext = context.getAttribute("userMap");
        HashMap<String,String> userMap = new HashMap();

        if (userMapInContext==null){
            userMap.put(username,password);
            context.setAttribute("userMap",userMap);
            request.setAttribute("msg","注册成功");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }else {
            userMap = (HashMap<String, String>) userMapInContext;

            if (userMap.containsKey(username)){
                // 当前用户名以存在
                request.setAttribute("msg","当前用户名已存在");
                request.getRequestDispatcher("/register.jsp").forward(request,response);
            }else {
                userMap.put(username,password);
                context.setAttribute("userMap",userMap);
                request.setAttribute("msg","注册成功");
                request.getRequestDispatcher("/login.jsp").forward(request,response);
            }
        }
    }



    private void generateCode(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        Random random = new Random(System.currentTimeMillis());
        StringBuilder sb = new StringBuilder();

        char sc = 'a';
        for (int i = 0; i <= 4; i++) {
            int str = random.nextInt(62);
            if (str < 10) {
                sb.append(str);
            } else if (str < 36) {
                sc = (char) ((str - 10) + 'a');
                sb.append(sc);
            } else {
                sc = (char) ((str - 36) + 'A');
                sb.append(sc);
            }
        }

        BufferedImage image = new BufferedImage(60, 20, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.getGraphics();
        graphics.setColor(Color.GRAY);
        graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("Time", Font.BOLD, 18));
        graphics.drawString(sb.toString(), 3, 17);
        graphics.dispose();
        ImageIO.write(image, "JPG", response.getOutputStream());
        System.out.println("生成的验证码：" + sb);

        // 将验证码保存到session
        request.getSession().setAttribute("code", sb.toString());
    }
}